import logger from 'src/common/utils/log';
import { CreatePlayerSignupDto } from '../dto/create-player-signup.dto';
import {
  Controller,
  Post,
  UsePipes,
  ValidationPipe,
  Body,
  Get,
  Query,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiQuery,
  ApiResponse,
} from '@nestjs/swagger';
import { PlayerSignupService } from '../service/player-signup.service';

import { playerSignupTypeEnum } from 'src/common/constant/enums';
import {
  SearchPlayerDto,
  SearchPlayerResponseDto,
} from '../dto/search-player.dto';
import { Auth } from 'src/common/decorators/auth.decorator';
import { RequirePermission } from 'src/common/decorators/permission.decorator';

@Auth()
@ApiBearerAuth()
@Controller('player-signup')
export class PlayerSignupController {
  constructor(private readonly playerSignupService: PlayerSignupService) {}

  @RequirePermission('1002002')
  @Post('add')
  @ApiOperation({ summary: '添加选手报名信息' })
  @ApiBody({ type: CreatePlayerSignupDto })
  @ApiOkResponse({ description: '选手报名信息添加成功' })
  @ApiResponse({ status: 400, description: '无效的请求数据' })
  @UsePipes(new ValidationPipe({ transform: true }))
  add(@Body() createPlayerSignupDto: CreatePlayerSignupDto) {
    return this.playerSignupService.handleSinglePlayer(createPlayerSignupDto);
  }

  @RequirePermission('1002003')
  @Post('update')
  @UsePipes(new ValidationPipe({ transform: true }))
  @ApiOperation({ summary: '更新选手报名信息' })
  @ApiBody({ type: CreatePlayerSignupDto })
  @ApiOkResponse({ description: '选手报名信息更新成功' })
  @ApiResponse({ status: 400, description: '无效的请求数据' })
  update(@Body() createPlayerSignupDto: CreatePlayerSignupDto) {
    // logger.info(updatePlayerSignupDto);
    return this.playerSignupService.handleSinglePlayer(createPlayerSignupDto);
  }
  @RequirePermission('1002004')
  @Get('delete')
  @ApiOperation({ summary: '删除选手报名信息' })
  @ApiQuery({
    name: 'key',
    type: String,
    required: true,
    description: '选手报名的唯一值',
  })
  @ApiOkResponse({ description: '选手报名信息删除成功' })
  @ApiResponse({ status: 400, description: '无效的查询参数' })
  delete(@Query('key') key: string) {
    return this.playerSignupService.removeByKey(key);
  }

  @RequirePermission('1002001')
  @Post('search')
  @UsePipes(new ValidationPipe({ transform: true }))
  @ApiOperation({ summary: '搜索选手报名信息' })
  @ApiBody({ type: SearchPlayerDto })
  @ApiOkResponse({
    description: '搜索成功',
    type: [SearchPlayerResponseDto],
  })
  @ApiResponse({ status: 400, description: '无效的查询参数' })
  async search(
    @Body() reqParam: SearchPlayerDto,
  ): Promise<SearchPlayerResponseDto> {
    // return null;
    const {
      param: { categoryId, eventIds, name: playerName, parentPlayerId },
      page,
      size,
    } = reqParam;
    const normalizedEventIds = Array.isArray(eventIds)
      ? eventIds
      : eventIds !== undefined
        ? [eventIds]
        : [];
    logger.info(
      `开始 playerSignupController.search categoryId： ${categoryId} eventIds: ${normalizedEventIds} playerName: ${playerName} parentPlayerId: ${parentPlayerId}`,
    );
    const signupPlayers = await this.playerSignupService.searchPlayers(
      page,
      size,
      {
        categoryId,
        eventIds: normalizedEventIds,
        playerName,
        type: playerSignupTypeEnum.single,
        parentPlayerId,
      },
    );
    return signupPlayers;
  }
}
